<?php
/**
 * User: hongta
 * Date: 4/5/14
 * Time: 11:43 AM
 */

class OrganizerController extends OrgController{
    public function actionLogin(){
        Yii::import('application.modules.bookingsystem.components.*');
        $this->layout = false;
        $form_login = new AdminLoginForm();
        if(isset($_POST['AdminLoginForm'])){
            $form_login->attributes = $_POST['AdminLoginForm'];
            if($form_login->validate()){
//                date_default_timezone_set('UTC');
//                $current_date = date("Y-m-d");
//                Event::model()->updateAll(array('status'=>STATUS_EXPIRED),'start_date <"'.$current_date.'"');


                $this->redirect(Yii::app()->createAbsoluteUrl('organizer/index'));
            }
        }
        $this->renderPartial('login',array('model'=> $form_login));
    }
    public function actionIndex(){
        $this->layout = false;
        date_default_timezone_set('UTC');

        $start_date = date('Y-m-d', time());
        // var_dump($start_date);die();
        // Get Current Language
        //Define new Criteria
        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->with = 'event';
        $criteria->join = 'JOIN events e ON t.event_id = e.id INNER JOIN event_categories c ON e.category_id = c.id';

        $criteria->compare('t.language_id', 1);
        $criteria->compare('e.active_status', STATUS_ACTIVE);
        // $criteria->addCondition('t.language_id = ' . $languages->id . ',e.start_date ='.$start_date .' OR t.language_id ='.$languages->id.',e.priority>0,e.start_date>' . $start_date);
        $criteria->compare('e.start_date', $start_date);
        $criteria2 = new CDbCriteria();
        $criteria2->condition = 't.language_id = :lang_id AND e.start_date >= :start_date AND e.priority IS NOT NULL';
        $criteria2->params = array(':lang_id' => 1,':start_date' => $start_date);
        // $criteria->order = 'e.priority DESC';
        $criteria->order = 'e.priority DESC , e.start_date,e.start_time,t.main_title ASC';
        $criteria->mergeWith($criteria2, 'OR');
        $event = Translate::model()->findAll($criteria);
        //Get all item from gallery of an event
        foreach ($event as $key => $value) {
            $image = Item::model()->findByAttributes(array('gallery_id' => $value->event->gallery_id, 'cover_image' => 1));
            //Assign cover image
            if ($image != null) {
                $value['event']['cover_image'] = $image->name;
            }
        }

        $this->render('index',array(
            'data' => $event,

        ));
    }
    public function actionGetticket(){
        $this->layout = false;
        if(isset($_POST['event_id'])){
            $event_ticket = Event::model()->with('tickets')->findByPk($_POST['event_id']);
//            $customer = new Customer();
//            $m_direct_book = new DirectBook();
//            $m_organizer = new OrganizerOrder();
            echo $this->renderPartial('_ticket_tab',array(
                'data' => $event_ticket,
//                'model_customer' => $customer,
//                'model_direct_book' => $m_direct_book,
//                'model_organizer' => $m_organizer
            ));
            Yii::app()->end();
        }
        if(isset($_POST['OrganizerOrder']) || $_POST['Customer'] || $_POST['DirectBook']){

            $customer = new Customer();
            $customer->attributes = $_POST['Customer'];
            $customer->email = $_POST['Customer']['email'];
            $customer->first_name = $_POST['Customer']['first_name'];
            $customer->last_name = $_POST['Customer']['last_name'];
            $customer->phone = $_POST['Customer']['phone'];
            if($customer->attributes != null){
                $customer->save();
            }


            $organizer_order = new OrganizerOrder();
            $organizer_order->attributes = $_POST['OrganizerOrder'];

            $organizer_order->save();



            foreach($_POST['DirectBook'] as $key => $value){
//                var_dump($value['price']);
                for($i = 0; $i < (int)$value['quantity']; $i++){
                    $book_direct = new DirectBook();
                    $book_direct->attributes = $value;

                    $book_direct->organizer_order_id = $organizer_order->id;
                    $book_direct->event_id = $_POST['OrganizerOrder']['event_id'];
                    $book_direct->quantity = 1;
                    $book_direct->price = $value['price'];
                    $book_direct->barcode_hash = substr(microtime(),2,8).substr(time(),6);
                    $book_direct->save();
                }
            }
            $organizer_order_data = OrganizerOrder::model()->with('event','detail_orders')->findByPk($organizer_order->id);
            echo $this->renderPartial('_barcode',array('data' => $organizer_order_data));

        }
    }
    public function actionCheckin(){
        $this->layout = false;
        if(isset($_POST['barcode'])){
//            var_dump($_POST['barcode']);die();
            $bar_code = substr($_POST['barcode'],0,12);
//            echo $bar_code;die;
            $result = DirectBook::model()->findByAttributes(array('barcode_hash' =>$bar_code,'checkin_status' => 0));
            if(isset($result))
            {
                $result->checkin_status = 1;
                $result->save();
                echo CJSON::encode(0);
            }
            else{
                echo CJSON::encode(1);
            }
            Yii::app()->end();
        }
        $this->render('checkin');
    }

}